#include "../StdInc.h"
#include "../../TradeService/database/DataBase.h"
#include "../../TradeService/database/DBCommon.h"
#include "../../TradeService/database/FrozenHelper.h"

namespace GSTrader
{
	namespace TradeService
	{
		CDataBaseProxy::CDataBaseProxy()
		{
			CDataBase* pHelper = CDataBase::GetInstance();
			if (pHelper)
			{
				m_pDBCommon = pHelper->m_pDBCommon;
			}
		}

		CDataBaseProxy::~CDataBaseProxy()
		{
			CDataBase::ReleaseInstance();
		}

		int CDataBaseProxy::GetNextGuid(int nType)
		{
			return m_pDBCommon->GetNextPrimaryKey(nType);
		}

		int CDataBaseProxy::HasOrderByStratgegy(int nStrategyId, int nInstrumentId, bool bNoTrade, int nOpenClose, int nSide)
		{
			return m_pDBCommon->HasOrderByStratgegy(nStrategyId, nInstrumentId, bNoTrade, nOpenClose, nSide);
		}

		bool CDataBaseProxy::GetOrderByStrategy(int nStrategyId, int nInstrumentId, bool bNoTrade, int nOpenClose, int nSide, vector<COrder>& vOrder)
		{
			return m_pDBCommon->GetOrderByStrategy(nStrategyId, nInstrumentId, bNoTrade, nOpenClose, nSide, vOrder);
		}

		void CDataBaseProxy::GetPosition(vector<CPosition>& vPosition, string strInstrumentID)
		{
			m_pDBCommon->GetPosition(-1, -1, -1, strInstrumentID, vPosition);
		}

		bool CDataBaseProxy::GetInstrumentClass(int nProviderId, vector<CInstrumentClass>& vInstrumentClass)
		{
			return m_pDBCommon->GetInstrumentClass(nProviderId, vInstrumentClass);
		}

		int CDataBaseProxy::GetPositionQty(int nProviderId, int nInstrumentId, int nLongShort)
		{
			return m_pDBCommon->GetPositionQty(nProviderId, nInstrumentId, nLongShort);
		}

		bool CDataBaseProxy::GetAutoOpenCloseOrder(COrder& order, vector<COrder*>& vOrder)
		{
			return m_pDBCommon->GetAutoOpenCloseOrder(order, vOrder);
		}

		bool CDataBaseProxy::AutoOpenClose(int nProviderId, string strInstrumentID, int nSide, int nVol,
		                                   int nHedgeFlag, bool bOpenNew, vector<CAutoOpenClose>& vOpenClose)
		{
			CDataBase* pHelper = CDataBase::GetInstance();
			if (pHelper)
			{
				CDatabaseAttach attach(pHelper);
				return m_pDBCommon->AutoOpenClose(nProviderId, strInstrumentID, nSide, nVol, nHedgeFlag, bOpenNew, vOpenClose);
			}

			return false;
		}

		bool CDataBaseProxy::GetTradingAccount(int nProviderId, CTradingAccount& account)
		{
			return m_pDBCommon->GetTradingAccount(nProviderId, account);
		}

		bool CDataBaseProxy::GetTradingAccount(int nProviderId, vector<CTradingAccount>& vTradingAccount)
		{
			return m_pDBCommon->GetTradingAccount(nProviderId, vTradingAccount);
		}

		bool CDataBaseProxy::GetPosition(int nProviderId, vector<CPosition>& positions)
		{
			return m_pDBCommon->GetPosition(nProviderId, -1, -1, "", positions);
		}

		bool CDataBaseProxy::GetPosition(int nProviderId, int nInstrumentId, int nLongShort, vector<CPosition>& positions)
		{
			return m_pDBCommon->GetPosition(nProviderId, nInstrumentId, nLongShort, "", positions);
		}

		bool CDataBaseProxy::GetPositionDetail(int nProviderId, vector<CPositionDetail>& vDetail)
		{
			return m_pDBCommon->GetPositionDetail(nProviderId, -1, "", vDetail);
		}

		bool CDataBaseProxy::GetPositionDetail(int nProviderId, int nInstrumentId, vector<CPositionDetail>& vDetail)
		{
			return m_pDBCommon->GetPositionDetail(nProviderId, nInstrumentId, "", vDetail);
		}

		bool CDataBaseProxy::GetOrder(int nId, COrder& order)
		{
			return m_pDBCommon->GetOrder(nId, order);
		}

		bool CDataBaseProxy::GetOrder(int nProviderId, int nInstrument, int nStatus, vector<COrder>& vOrders, int nType)
		{
			return m_pDBCommon->GetOrder(nProviderId, nInstrument, "", nStatus, vOrders, nType);
		}

		bool CDataBaseProxy::GetTrade(int nProviderId, int nInstrument, vector<CTrade>& vTrade)
		{
			return m_pDBCommon->GetTrade(nProviderId, nInstrument, "", vTrade);
		}

		bool CDataBaseProxy::GetNoTradeOrder(int nProviderId, string strInstrumentID, string strExchangeID, int nStrategyID, vector<COrder>& vOrder)
		{
			return m_pDBCommon->GetNoTradeOrder(nProviderId, -1, strInstrumentID, strExchangeID, nStrategyID, vOrder);
		}

		bool CDataBaseProxy::GetNoTradeOrder(int nProviderId, int nInstrumentId, vector<COrder>& vOrders)
		{
			return m_pDBCommon->GetNoTradeOrder(nProviderId, nInstrumentId, "", "", -1, vOrders);
		}

		bool CDataBaseProxy::GetExchange(int nProviderId, vector<CExchange>& vExchange)
		{
			return m_pDBCommon->GetExchange(nProviderId, vExchange);
		}

		CReplaceOrder* CDataBaseProxy::GetReplaceOrder(COrder* pSrcOrder, COrder* pNewOrder, ReplaceOrderMethod* pMethod)
		{
			return m_pDBCommon->GetReplaceOrder(pSrcOrder, pNewOrder, pMethod);
		}

		bool CDataBaseProxy::CreateOrder(vector<COrder*>& vOrder,
		                                 int nProviderId,
		                                 string strInstrumentId,
		                                 string strMarketID,
		                                 double dPrice,
		                                 int nSide,
		                                 int nVol,
		                                 int nHedgeFlag,
		                                 int nOpenClose,
		                                 bool bAutoOpenClose,
		                                 int nOrderPriceType,
		                                 string strDirection,
		                                 int nStrategyId,
		                                 int nPortfolioId,
		                                 double dStopPrice,
		                                 double dStopProfitPrice
		                                )
		{
			return m_pDBCommon->CreateOrder(vOrder, nProviderId, strInstrumentId, strMarketID, dPrice, nSide, nVol, nHedgeFlag, nOpenClose, bAutoOpenClose,
			                                nOrderPriceType,
			                                strDirection, nStrategyId, nPortfolioId, dStopPrice, dStopProfitPrice);
		}

		int CDataBaseProxy::GetForzenQty(int nProviderId, int nInstrumentId, int nSide, int nHegdeFlag)
		{
			CDataBase* pHelper = CDataBase::GetInstance();
			if (pHelper)
			{
				CDatabaseAttach attach(pHelper);
				return pHelper->m_pPrepareForzenHelper->GetForzenQty(nProviderId, nInstrumentId, nSide, nHegdeFlag);
			}

			return 0;
		}

		bool CDataBaseProxy::GetExistingOrder(COrder* pOrder, vector<COrder>& vOrder)
		{
			CDataBase* pHelper = CDataBase::GetInstance();
			if (pHelper)
			{
				CDatabaseAttach attach(pHelper);
				return pHelper->m_pDBCommon->GetExistOrderByOrder(pOrder->m_nProviderId, pOrder, vOrder);
			}

			return false;
		}


		int CDataBaseProxy::GetClosePosition(int nProviderId, int nInstrumentId, string strInstrumentId, int nLongShort, int nHegde, int nOpenClose)
		{
			CDataBase* pHelper = CDataBase::GetInstance();
			if (pHelper)
			{
				CDatabaseAttach attach(pHelper);
				return pHelper->m_pDBCommon->GetClosePosition(nProviderId, nInstrumentId, strInstrumentId, nLongShort, nHegde, nOpenClose);
			}

			return 0;
		}

		bool CDataBaseProxy::GetOrderByRowId(int nRowId, COrder* pOrder)
		{
			return m_pDBCommon->GetOrderByRowId(nRowId, pOrder);
		}

		bool CDataBaseProxy::GetAccountTransferSerial(int nProviderId, vector<CAccountTransferSerial*>& vSeries)
		{
			return m_pDBCommon->GetAccountTransferSerial(nProviderId, vSeries);
		}

		CDepthMarketData* CDataBaseProxy::GetMarketDataById(int nInstrumentId)
		{
			return m_pDBCommon->GetMarketDataById(nInstrumentId);
		}

		CDepthMarketData* CDataBaseProxy::GetMarketDataBySymbol(CInstrumentLongCode& code)
		{
			return m_pDBCommon->GetMarketDataBySymbol(code.m_strInstrumentId, code.m_strExchangeId);
		}

		CDepthMarketData* CDataBaseProxy::GetMarketDataBySymbol(string strInstrumentId, string strExchangeId)
		{
			return m_pDBCommon->GetMarketDataBySymbol(strInstrumentId, strExchangeId);
		}

		bool CDataBaseProxy::UpdateMarketData(CDepthMarketData* pDepthMarketData)
		{
			return m_pDBCommon->UpdateMarketData(pDepthMarketData);
		}

		CDepthMarketData* CDataBaseProxy::UpdateDepthMarketData(CDepthMarketData* pDepthMarketData)
		{
			return m_pDBCommon->UpdateDepthMarketData(pDepthMarketData);
		}

		bool CDataBaseProxy::AddInstrument(CInstrument* pInstrument)
		{
			return m_pDBCommon->AddInstrument(pInstrument);
		}

		bool CDataBaseProxy::GetInstrumentProvider(int nInstrumentId, vector<int>& vProviderId)
		{
			return m_pDBCommon->GetInstrumentProvider(nInstrumentId, vProviderId);
		}

		bool CDataBaseProxy::GetAccountInstrument(int nProviderId, vector<CInstrument*>& vInstrument)
		{
			assert(false);
			return false;
		}

		CInstrument* CDataBaseProxy::GetInstrumentBySymbol(string strInstrumentId, string strExchangeId)
		{
			return m_pDBCommon->GetInstrumentBySymbol(strInstrumentId, strExchangeId);
		}

		CInstrument* CDataBaseProxy::GetInstrumentById(int nInstrumentId)
		{
			return m_pDBCommon->GetInstrumentById(nInstrumentId);
		}

		void CDataBaseProxy::GetInstrumentByExchange(string strExchangeId, vector<CInstrument*>& vInstrument)
		{
			m_pDBCommon->GetInstrumentByExchange(strExchangeId, vInstrument);
		}

		void CDataBaseProxy::GetAllInstrument(vector<CInstrument*>& vInstrument, bool bSort)
		{
			m_pDBCommon->GetAllInstrument(vInstrument, bSort);
		}

		void CDataBaseProxy::GetAllMarketData(vector<CDepthMarketData*>& vAllMarketData)
		{
			m_pDBCommon->GetAllMarketData(vAllMarketData);
		}

		size_t CDataBaseProxy::GetInstrumentCount()
		{
			return m_pDBCommon->m_mapInstrumentById.size();
		}

		void CDataBaseProxy::Init(CMsgQueue* pServiceQueue)
		{
			m_pDBCommon->m_pServiceQueue = pServiceQueue;
		}

		void CDataBaseProxy::SetDataBaseDyncCalc(bool bDyncCalc)
		{
			m_pDBCommon->m_pDataBase->m_bDyncCalc = bDyncCalc;
		}

		bool CDataBaseProxy::GetDataBaseDyncCalc()
		{
			return m_pDBCommon->m_pDataBase->m_bDyncCalc;
		}
	}
}